package com.example.vaultmysql.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

/**
 * 连接池配置实体类
 * 
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "connection_pool")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConnectionPool {

    @Id
    @Column(length = 50)
    private String id;

    @Column(name = "database_id", length = 50, nullable = false)
    private String databaseId;

    @Column(name = "max_pool_size", nullable = false)
    private Integer maxPoolSize = 20;

    @Column(name = "min_idle", nullable = false)
    private Integer minIdle = 5;

    @Column(name = "connection_timeout", nullable = false)
    private Integer connectionTimeout = 30000;

    @Column(name = "idle_timeout", nullable = false)
    private Integer idleTimeout = 600000;

    @Column(name = "max_lifetime", nullable = false)
    private Integer maxLifetime = 1800000;

    @Column(name = "auto_commit", nullable = false)
    private Boolean autoCommit = true;

    /**
     * 构造函数
     */
    public ConnectionPool(String id, String databaseId) {
        this.id = id;
        this.databaseId = databaseId;
    }

    /**
     * 构造函数 - 自定义配置
     */
    public ConnectionPool(String id, String databaseId, Integer maxPoolSize, 
                         Integer minIdle, Integer connectionTimeout, 
                         Integer idleTimeout, Integer maxLifetime, Boolean autoCommit) {
        this.id = id;
        this.databaseId = databaseId;
        this.maxPoolSize = maxPoolSize;
        this.minIdle = minIdle;
        this.connectionTimeout = connectionTimeout;
        this.idleTimeout = idleTimeout;
        this.maxLifetime = maxLifetime;
        this.autoCommit = autoCommit;
    }

    /**
     * 验证配置参数
     */
    public boolean isValid() {
        return maxPoolSize > 0 && 
               minIdle >= 0 && 
               minIdle <= maxPoolSize &&
               connectionTimeout > 0 &&
               idleTimeout > 0 &&
               maxLifetime > 0;
    }

    /**
     * 获取配置摘要
     */
    public String getConfigSummary() {
        return String.format("Pool[max=%d, min=%d, timeout=%dms]", 
                           maxPoolSize, minIdle, connectionTimeout);
    }
}